package com.vinxin.app.utils

import jakarta.servlet.http.HttpServletRequest
import org.springframework.http.HttpHeaders

class DataUtils {
    companion object {
        @JvmStatic
        fun extractHeaders(request: HttpServletRequest): Map<String, String?> {
            val cookies = request.cookies ?: emptyArray()
            val cookieMap = cookies.associate { cookie ->
                cookie.name to cookie.value
            }
            return hashMapOf(
                "Priority" to "u=1, i",
                "X-Requested-With" to "XMLHttpRequest",
                "Zp_token" to cookieMap["bst"],
            ) + request.headerNames.toList()
                .filter { !"referer".equals(it, ignoreCase = true) && !"host".equals(it, ignoreCase = true) && !"origin".equals(it, ignoreCase = true) }
                .associateWith { request.getHeader(it) }
                .filterValues { it.isNotBlank() }
        }

        @JvmStatic
        fun map2HttpHeaders(headersMap: Map<String, String?>): HttpHeaders {
            return HttpHeaders().apply {
                headersMap.forEach { (key, value) -> value?.let { add(key, it) } }
            }
        }
    }
}